package mySeleniumDriver;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class DataTable extends BaseClass {

  public String getDataTableValue(String columnname, int rownum) throws IOException, BiffException {
    String dataTableValue = "COLUMN NOT FOUND";
    try {
      Workbook readWB = Workbook.getWorkbook(new File(dataTableExcel));
      Sheet dataSheet = readWB.getSheet(dataTableSheet);
      for (int i = 0; i < dataSheet.getColumns(); i++) {
        if (dataSheet.getCell(i, 0).getContents().toString()
            .equalsIgnoreCase(columnname.substring(3, columnname.length()))) {
          // Column found in Data sheet
          dataTableValue = dataSheet.getCell(i, rownum).getContents().toString();
          if (dataTableValue.trim() == "") {
            dataTableValue = "NO DATA IN CELL";
          }
          break;
        }
      }
      readWB.close();

    } catch (IOException e) {
      log("error", e.getMessage());
    }
    return dataTableValue;
  }

  public void putValueToDataTable(String columnname, int rownum, String value) throws IOException,
      BiffException, RowsExceededException, WriteException {
    try {
      Workbook writeWB = Workbook.getWorkbook(new File(dataTableExcel));
      WritableWorkbook copy = Workbook.createWorkbook(new File(dataTableExcel), writeWB);
      WritableSheet writableSheet = copy.getSheet(dataTableSheet);
      boolean columnFound = false;
      int writeToColumn = 0;
      for (int i = 0; i < writableSheet.getColumns(); i++) {
        if (writableSheet.getCell(i, 0).getContents().toString()
            .equalsIgnoreCase(columnname.substring(3, columnname.length()))) {
          // Column found in Data sheet.
          columnFound = true;
          writeToColumn = i;
          break;
        }
      }

      if (columnFound == false) {
        // Add the column name to the data table
        writeToColumn = writableSheet.getColumns();
        Label newDataTableColumn =
            new Label(writeToColumn, 0, columnname.substring(3, columnname.length()));
        writableSheet.addCell(newDataTableColumn);
        log("info", "New column added to data table.");
      }

      Label label = new Label(writeToColumn, rownum, value);
      writableSheet.addCell(label);
      copy.write();
      copy.close();
      log("info", "Value inserted into data table");
      writeWB.close();
    } catch (IOException e) {
      log("error", e.getMessage());
    }
  }

  public int getDataTableRowCount() throws IOException, BiffException {
    int dataTableRowCount = 0;
    try {
      Workbook readRowCountOfWB = Workbook.getWorkbook(new File(dataTableExcel));
      Sheet dataCountSheet = readRowCountOfWB.getSheet(dataTableSheet);
      dataTableRowCount = dataCountSheet.getRows() - 1; // exclude header
      log("info", "No of rows in Data table =" + dataTableRowCount);
      readRowCountOfWB.close();

    } catch (IOException e) {
      log("error", e.getMessage());
    }
    return dataTableRowCount;
  }


}
